/**
 * Read web server data and analyse
 * hourly access patterns.
 * 
 * @author David J. Barnes and Michael Kolling.
 * @version 2008.03.30
 */
public class LogAnalyzer
{
    // Where to calculate the hourly access counts.
    private int[] hourCounts;
    // Use a LogfileReader to access the data.
    private LogfileReader reader;

    /**
     * Create an object to analyze hourly web accesses.
     */
    public LogAnalyzer()
    { 
        // Create the array object to hold the hourly
        // access counts.
        hourCounts = new int[24];
        // Create the reader to obtain the data.
        reader = new LogfileReader();
    }

    /**
     * Analyze the hourly access data from the log file.
     */
    public void analyzeHourlyData()
    {
        while(reader.hasMoreEntries()) {
            LogEntry entry = reader.nextEntry();
            int hour = entry.getHour();
            hourCounts[hour]++;
        }
    }

    /**
     * Print the hourly counts.
     * These should have been set with a prior
     * call to analyzeHourlyData.
     */
    public void printHourlyCounts()
    {
        System.out.println("Hr: Count");
        //for(int hour = 0; hour < hourCounts.length; hour++) {
        //    System.out.println(hour + ": " + hourCounts[hour]);
        //}
        int index = 0;
        while(index < hourCounts.length){
            System.out.println(index + ": " + hourCounts[index]);
            index++;
        }
    }
    
    /**
     * Print the lines of data read by the LogfileReader
     */
    public void printData()
    {
        reader.printData();
    }
    
    /**
     * Return the total of all visits.
     */
    public int numberOfAccesses(){
        int total = 0;
        // tel de waarde in elk element van hourCounts op bij de waarde total.
        for(int index = 0;index < hourCounts.length; index++){
            total += hourCounts[index];
        }
        return total;
    }
    
    /**
     * Return the busiest hour.
     */
    public int busiestHour(){
        int bh = 0;
        int bhCount = 0;
        // tel de waarde in elk element van hourCounts op bij de waarde total.
        for(int index = 0;index < hourCounts.length; index++){
            if(bhCount < hourCounts[index])
                bhCount = hourCounts[index];
                bh = index;
        }
        return bh;
    }
    
    /**
     * Return the quietest hour.
     */
    public int quietestHour(){
        int qh = 0;
        int qhCount = 0;
        // tel de waarde in elk element van hourCounts op bij de waarde total.
        for(int index = 0;index < hourCounts.length; index++){
            if(qhCount > hourCounts[index] || qhCount == 0)
                qhCount = hourCounts[index];
                qh = index;
        }
        return qh;
    }
    
    /**
     * Return the busiest periode of two following hours.
     */
    public int busiestPeriod(){
        int busiestTotal = 0;
        int p1 = 0;
        int p2 = 0;
        int busiestPeriode = 0;
        
        for(int index = 0;index < hourCounts.length-1; index++){
            p1 = hourCounts[index];
            p2 = hourCounts[index + 1];
            System.out.println("Periode '" + index + "' + '" + (index+1) + "' = " + (p1+p2));
            if((p1+p2) > busiestTotal){
                busiestPeriode = index;
                busiestTotal = p1+p2;
            }
        }
        return busiestPeriode;
    }
}

